package com.example.template.core.controller;
/**
 * Description: //TODO
 * Created by meko on 19-3-15下午6:36
 */



import com.example.template.core.entity.BaseEntity;
import com.example.template.core.model.AppJsonMessage;
import com.example.template.core.model.CommonConst;
import com.example.template.core.service.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *@ClassName BaseControllerCrud
 *@Description TODO
 *@Author meko
 *@Date 19-3-15 下午6:36
 *@Version 1.0
 **/
public class BaseControllerCrud<T extends BaseEntity> extends BaseController {



    @Autowired
    protected BaseService<T> baseService;


    @ApiOperation(value = "查询所有实体")
    @GetMapping("")
    public AppJsonMessage getAllEntity(final T t,@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "20") Integer size){
        List<T> dataList = baseService.selectAll(t,page,size);
        return this.getJsonMessage(CommonConst.SUCCESS,dataList);
    }

    @ApiOperation(value = "根据实体ID查询实体")
    @GetMapping("/{{id}}")
    public AppJsonMessage getEntityByEntity(@PathVariable("{id}") Integer id){
        T entity = baseService.getEntityById(id);
        return this.getJsonMessage(CommonConst.SUCCESS,entity);
    }

    @ApiOperation(value = "新增实体",notes = "addEntity")
    @PostMapping("")
    public AppJsonMessage addEntity(@RequestBody final T t)throws Exception{
        int i = baseService.addEntity(t);
        return this.getJsonMessage(CommonConst.SUCCESS,i);
    }

    @ApiOperation(value = "更新实体",notes = "updateEntity")
    @PutMapping("")
    public AppJsonMessage updateEntity(@RequestBody final T t){
        int i = baseService.updateEntity(t);
        return this.getJsonMessage(CommonConst.SUCCESS,i);
    }

    @ApiOperation(value = "根据id删除实体",notes = "deleteEntity")
    @DeleteMapping("/{{id}}")
    public AppJsonMessage deleteEntity(@PathVariable("{id}") Integer id){
        int i = baseService.deleteEntityById(id);
        return this.getJsonMessage(CommonConst.SUCCESS,i);
    }

}
